package com.hirsi.common.db;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.TemplateType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;

import java.util.Collections;

public class CodeAutoGenerator {

    final static String url = "jdbc:mysql://localhost:3306/ztoke";

    final static String username = "root";

    final static String password = "123456";

    public static void main(String[] args) {

        String projectName = "lib\\common-lib";

        String moduleName = "manager";

        String[] tableName = new String[]{"ztk_manager"};

        String[] tablePrefix = new String[]{"ztk_"};

        FastAutoGenerator.create(url, username, password)
                .globalConfig(builder -> {
                    builder.author("封夕")
                            .outputDir(System.getProperty("user.dir") + "\\" + projectName + "\\src\\main\\java\\")
                            .disableOpenDir();
                })
                .packageConfig(builder -> {
                    builder.parent("com.hirsi.common.db")
                            .moduleName(moduleName)
                            .entity("entity")
                            .pathInfo(Collections.singletonMap(OutputFile.xml, System.getProperty("user.dir") + "\\" + projectName + "\\src\\main\\java\\com\\hirsi\\common\\db\\" + moduleName + "\\mapper"));
                })
                .strategyConfig(builder -> {
                    builder.addTablePrefix(tablePrefix)
                            .addInclude(tableName)
                            .enableSkipView();
                    builder.entityBuilder()
                            .enableFileOverride()
                            .enableLombok()
                            .enableChainModel()
                            .enableTableFieldAnnotation()
                            .enableActiveRecord()
                            .versionColumnName("version")
                            .versionPropertyName("version")
                            .logicDeleteColumnName("del_flag")
                            .logicDeletePropertyName("delFlag")
                            .addTableFills(new Column("create_time", FieldFill.INSERT), new Column("create_user", FieldFill.INSERT), new Column("update_time", FieldFill.INSERT_UPDATE), new Column("update_user", FieldFill.INSERT_UPDATE))
                            .idType(IdType.AUTO)
                            .build();
                    builder.serviceBuilder()
                            .formatServiceImplFileName("I%s")
                            .build();
                    builder.mapperBuilder()
                            .formatMapperFileName("I%s")
                            .formatXmlFileName("I%s")
                            .enableFileOverride()
                            .enableBaseResultMap()
                            .enableBaseColumnList()
                            .enableMapperAnnotation()
                            .build();
                })
                .templateConfig(builder -> builder.disable(TemplateType.CONTROLLER))
                .templateEngine(new FreemarkerTemplateEngine()).execute();
    }
}
